//
//  KABCosPublishCRView.m
//  kabu
//
//  Created by XiangDongMBP on 2017/2/8.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABCosPublishCRView.h"
#import "GlobalHeader.h"
#import "KABModel.h"
#import "NSString+XDLink.h"
#import "UIColor+XDHex.h"

@interface KABCosPublishCRView ()<UITextViewDelegate>
@property (nonatomic, assign) BOOL inputTopicCharacter;
@property (nonatomic, assign) BOOL inputAtCharacter;
@end

@implementation KABCosPublishCRView

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.textView.linkTextAttributes = @{NSForegroundColorAttributeName : [UIColor xd_colorWithHex:KABGlobalColorTabBarItemColor]};
    self.textView.delegate = self;
}

#pragma mark - <UITextViewDelegate>
- (void)textViewDidChange:(UITextView *)textView {
    NSString *toBeString = textView.text;
    NSInteger maxLength = KABGlobalCosPubTitleLength;
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position || !selectedRange) {
        if (toBeString.length > maxLength) {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
            if (rangeIndex.length == 1) {
                textView.text = [toBeString substringToIndex:maxLength];
            } else {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                textView.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }

    // 键盘不在联想状态才进行判断
    if (!selectedRange) {
        self.pubModel.top_content = textView.text;
        self.pubModel.cursorPostion = textView.selectedRange;
        NSRange cursorPostion = textView.selectedRange;
        textView.attributedText = [self.pubModel.top_content xd_matchTopicString];
        textView.selectedRange = cursorPostion;
        //    if ((textView.text.length > self.pubModel.top_content.length) && self.inputTopicCharacter) {
        //        self.inputTopicCharacter = NO;
        //        if ([self.delegate respondsToSelector:@selector(kABCosPublishCRView:topicCharacterInput:)]) {
        //            [self.delegate kABCosPublishCRView:self topicCharacterInput:textView];
        //        }
        //    }
        if (self.inputTopicCharacter) {
            self.inputTopicCharacter = NO;
            if ([self.delegate respondsToSelector:@selector(kABCosPublishCRView:topicCharacterInput:)]) {
                [self.delegate kABCosPublishCRView:self topicCharacterInput:textView];
            }
        } else if (self.inputAtCharacter) {
            self.inputAtCharacter = NO;
            if ([self.delegate respondsToSelector:@selector(kABCosPublishCRView:atCharacterInput:)]) {
                [self.delegate kABCosPublishCRView:self atCharacterInput:textView];
            }
        }
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSLog(@"should  %@, %@", NSStringFromRange(range), text);
    if (range.length == 0) {    // 新增
        if ([text hasSuffix:@"#"]) {
            self.inputTopicCharacter = YES;
        } else if ([text hasSuffix:@"@"]) {
            self.inputAtCharacter = YES;
        }
    }
    
    return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    self.pubModel.top_content = textView.text;
}

- (void)setPubModel:(KABCosPublishModel *)pubModel {
    _pubModel = pubModel;
    self.textView.text = pubModel.top_content;
    self.textView.attributedText = [pubModel.top_content xd_matchTopicString];
    self.textView.selectedRange = pubModel.cursorPostion;
}

@end
